package com.tiyende.credit.loop.ui.activity

import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.common.CommonActivity
import com.tiyende.credit.loop.databinding.TiyendeActivityAuthenticationStepsBinding
import com.tiyende.credit.loop.databinding.TiyendeActivityAuthenticationStepsProgressItemBinding
import com.tiyende.credit.loop.event.TiyendeEventData
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.ui.contract.IViewContract
import com.tiyende.credit.loop.ui.fragment.TiyendeAuthenticationStepAccountFragment
import com.tiyende.credit.loop.ui.fragment.TiyendeAuthenticationStepCommonFragment
import com.tiyende.credit.loop.ui.fragment.TiyendeAuthenticationStepContactFragment
import com.tiyende.credit.loop.ui.presenter.TiyendeAuthenticationStepsPresenter
import com.tiyende.credit.loop.utils.TiyendePickContactUtils
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/18 18:51
 * @Annotation：进件流程
 */
open class TiyendeAuthenticationStepsActivity: CommonActivity(), IViewContract.ITiyendeAuthenticationView {
//    lateinit var binding: TiyendeActivityAuthenticationStepsBinding
    // 使用可空类型的 backing property
    private var _binding: TiyendeActivityAuthenticationStepsBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "Cannot access binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )

    var presenter: TiyendeAuthenticationStepsPresenter? = null
    //选择联系人
    var pickContact: TiyendePickContactUtils? = null
    override fun getTiyendeRootView(): View? {
        _binding = TiyendeActivityAuthenticationStepsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeBackBtnRes(R.mipmap.tiyende_back_white)
        setTiyendeRightBtnRes(R.mipmap.tiyende_customer_white)
        setTiyendeTextTitleVisibility(View.GONE)

        pickContact = TiyendePickContactUtils(this)
    }

    override fun initTiyendeListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            presenter?.let {
                val eventTitle = it.getProcessEventTitle()
                showCustomerDialog(eventTitle)
                addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_CUSTOMER,eventTitle)
            }
        }
    }

    override fun loadTiyendeData() {
        if (presenter == null){
            presenter = TiyendeAuthenticationStepsPresenter(this)
        }
        presenter?.loadProcessPageAction()
    }

    /**
     * 所有步骤已完成
     */
    override fun showTiyendeAuthenticationStepCompleteViews() {
        onIntent(TiyendeLimitReviewingActivity::class.java)
        finish()
    }

    /**
     * 移除所有步骤
     */
    override fun removeTiyendeAuthenticationStepAllViews() {
        _binding?.processCurrentLayout?.removeAllViews()
    }

    /**
     * 设置认证步骤进度
     */
    override fun showTiyendeAuthenticationStepProgress(total: Int, progress: Int) {
        _binding?.authenticationStepProgressLayout?.removeAllViews()
        for (i in 0..< total){
            val itemBinding = TiyendeActivityAuthenticationStepsProgressItemBinding.inflate(layoutInflater)
            val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
            params.width = resources.getDimension(com.lib.common.R.dimen.dp_19).toInt()
            if (i == total - 1){
                params.setMargins(0,0,resources.getDimension(com.lib.common.R.dimen.dp_0).toInt(),0)
            } else {
                params.setMargins(0,0,resources.getDimension(com.lib.common.R.dimen.dp_4).toInt(),0)
            }
            itemBinding.authenticationProgressItemView.layoutParams = params
            if (i <= progress){//进行中 && 已完成
                when (i) {
                    0 -> {
                        itemBinding.authenticationProgressItemView.shapeDrawableBuilder
                            .setSolidColor(Color.parseColor("#FF3ED9AB"))
                            .setTopLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_100))
                            .setBottomLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_100))
                            .setTopRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .setBottomRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .intoBackground()
                    }
                    total - 1 -> {
                        itemBinding.authenticationProgressItemView.shapeDrawableBuilder
                            .setSolidColor(Color.parseColor("#FF3ED9AB"))
                            .setTopLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .setBottomLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .setTopRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_100))
                            .setBottomRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_100))
                            .intoBackground()
                    }
                    else -> {
                        itemBinding.authenticationProgressItemView.shapeDrawableBuilder
                            .setSolidColor(Color.parseColor("#FF3ED9AB"))
                            .setTopLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .setBottomLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .setTopRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .setBottomRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                            .intoBackground()
                    }
                }
            } else {
                if (i == total - 1){
                    itemBinding.authenticationProgressItemView.shapeDrawableBuilder
                        .setSolidColor(Color.parseColor("#FF3C5951"))
                        .setTopLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                        .setBottomLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                        .setTopRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_100))
                        .setBottomRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_100))
                        .intoBackground()
                } else {
                    itemBinding.authenticationProgressItemView.shapeDrawableBuilder
                        .setSolidColor(Color.parseColor("#FF3C5951"))
                        .setTopLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                        .setBottomLeftRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                        .setTopRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                        .setBottomRightRadius(resources.getDimension(com.lib.common.R.dimen.dp_0))
                        .intoBackground()
                }
            }
            _binding?.authenticationStepProgressLayout?.addView(itemBinding.root)
        }
    }

    /**
     * 设置当前进行的步骤图标和名称
     */
    override fun showTiyendeAuthenticationCurrentStep(icon: Int, title: String) {
        _binding?.imageAuthenticationStepIcon?.setImageResource(icon)
        _binding?.txtAuthenticationStepTitle?.text = title
    }

    /**
     * 联系人
     */
    override fun showTiyendeAuthenticationStepContactView(data: JSONObject) {
        replaceContainerFragement(R.id.processCurrentLayout, TiyendeAuthenticationStepContactFragment(this,data))
    }

    /**
     * 法语区 绑卡
     */
    override fun showTiyendeAuthenticationStepBindWalletView(data: JSONObject) {
        replaceContainerFragement(R.id.processCurrentLayout, TiyendeAuthenticationStepAccountFragment(data))
    }

    /**
     * 通用样式
     */
    override fun showTiyendeAuthenticationStepCommonView(data: JSONObject) {
        replaceContainerFragement(R.id.processCurrentLayout, TiyendeAuthenticationStepCommonFragment(data))
    }

    /**
     * 提交
     */
    open fun onAuthenticationStepSubmitAction(params: JSONObject){
        presenter?.onAuthenticationStepSubmitAction(params)
    }

    /**
     * 流程提交成功
     * 需要发送通知刷新总流程
     */
    override fun showTiyendeAuthenticationStepSubmitSuccess() {
        //通知刷新总流程
        publishEventData(TiyendeEventData(TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESH))
    }

    override fun onTiyendePublishEvent(event: TiyendeEventData) {
        super.onTiyendePublishEvent(event)
        when(event.eventType){
            TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESHED -> {
                presenter?.loadProcessPageAction()
            }
            else -> {

            }
        }
    }

    override fun onStop() {
        super.onStop()
        _binding?.root?.clearFocus()
    }

    override fun onDestroy() {
        super.onDestroy()
        _binding = null
    }
}